package com.ocom.webapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.entity.access.EntityMjAreaInfo;
import com.ocom.common.entity.master.EntityMngUserInfo;
import com.ocom.common.resp.Result;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.AccessClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class RegionPageController {

    @Autowired
    private AccessClientService accessClientService;

    /*
     * @Description:片区列表（RegionPage/onShow）
     * @Author: lu 
     * @Date: 2022/4/20 11:14
     * @param jsonObject:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_regionPageOnShow)
    public Result regionPageOnShow(@RequestBody JSONObject jsonObject){
        return accessClientService.getRegionInfo(jsonObject);
    }

    /*
     * @Description:4.9.2片区列表（RegionPage/allonShow）
     * @Author: lu 
     * @Date: 2022/4/20 14:49
     * @param jsonObject:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_regionPageAllonShow)
    public Result regionPageAllonShow(@RequestBody JSONObject jsonObject){
        jsonObject.put("page",-1);
        jsonObject.put("size",-1);
        return accessClientService.getRegionInfo(jsonObject);
    }

    @PostMapping(AccessServiceUrl.URL_regionPageAadd)
    public Result regionPageAadd(@RequestBody JSONObject jsonObject){
        return accessClientService.newRegionInfo(jsonObject);
    }

    /*
     * @Description:片区修改（RegionPage/modify）
     * @Author: lu 
     * @Date: 2022/4/20 14:52
     * @param jsonObject:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_regionPageModify)
    public Result regionPageModify(@RequestBody JSONObject jsonObject){
        return accessClientService.updateRegionInfo(jsonObject);
    }

    @PostMapping(AccessServiceUrl.URL_regionPageDelete)
    public Result regionPageDelete(@RequestBody JSONObject jsonObject){
        //1、判断片区下是否存在下挂区域(调用接口getAreaInfo )；
        Result getAreaInfoResult=accessClientService.GetAreaInfo(jsonObject.getLong("comId"),jsonObject.getInteger("regionId"),jsonObject.getInteger("areaId"),jsonObject.getString("areaName"));
        if (getAreaInfoResult.getCode()==200){
            YoCiUser yoCiUser=SecurityUtils.getYoCiUser();
            List<EntityMjAreaInfo> entityMjAreaInfos= JSON.parseArray(JSON.toJSONString(getAreaInfoResult.getData()), EntityMjAreaInfo.class);
            //存在则先删除区域及对应的权限等相关信息（调用接口removeArea）,全部删除后删除mj_region_info 数据
            if (entityMjAreaInfos.size()>0){
                for (EntityMjAreaInfo entityMjAreaInfo : entityMjAreaInfos) {
                    Map<String,Object> map=new HashMap<>();
                    map.put("companyID",jsonObject.getLong("comId"));
                    map.put("P_ID",yoCiUser.getId());
                    map.put("areaNo",entityMjAreaInfo.getAreaId());
                    accessClientService.RemoveArea(map);
                }
            }
        }
        return accessClientService.removeRegionInfo(jsonObject);
    }
}
